#include<cstdlib>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>

using namespace std;
class String
{
static const size_t s_min_capacity;
private:
    char* _data;    //数据成员
    size_t size_;  //字符串长度
    size_t capacity_; //空间容量
    //重新分配内存的辅助函数
    void realloc_data(size_t new_cap){

        new_cap=std::max(new_cap,s_min_capacity);
        char* new_data=new char[new_cap+1];
        if(size_>0)
        {
            memcpy(new_data,_data,size_);
        }
        new_data[size_+1]='\0';
        delete []_data;
        _data=new_data;
        capacity_=new_cap;
    }
public:
    ~String(){}
    //默认构造函数
    String():size_(0),capacity_(s_min_capacity),_data(new char[capacity_+1])
    {
        _data=new char[capacity_+1];
        _data[0]='\0';

    }
    //c风格转换的构造
    String(const char* str){
        if(!str)
        {
            throw std::invalid_argument("null pointer");
        }
        size_=std::strlen(str);
        capacity_=std::max(size_,s_min_capacity);
        _data=new char[capacity_];
        memcpy(_data,str,size_+1);

    }
    //移动构造函数
    String(String&& other)noexcept: 
        _data(other._data),
        size_(other.size_),
        capacity_(other.capacity_)
    {
        other.size_=0;
        other._data=nullptr;
        other.capacity_=0;
    }
    //拷贝构造函数
    String(const String& other):_data(other._data),size_(other.size_),capacity_(other.capacity_)
    {
        _data=new char[capacity_+1];
        memcpy(_data,other._data,size_+1);
    }
    String& operator=(String&& other)noexcept
    {
        if(this!=&other)
        {
            other._data=nullptr;
            other.size_=0;
            other.capacity_=0;
        }
        return *this;
    }

    String& append(const char* str,size_t len)
    {
        if(!str)    throw std::invalid_argument("null pointer");
        if(size_+len>capacity_)
        {
           reserve((size_+len)*2);
        }
        memcpy(_data,str,size_+1);
        size_+=len;
        _data[size_]='\0';
        return *this;
       
    }
    String& append(const char* str)
    {
        if(!str)    throw std::invalid_argument("null pointer");
        return append(str,std::strlen(str));
    }
    //获取数据
    const char* c_str()const noexcept{return _data;}
    const char* data()const noexcept{return _data;}
    size_t capacity()const noexcept{return capacity_;}
    size_t size()const noexcept{return size_;}
    bool empty()const noexcept{return size_==0;}
    void reserve(size_t new_capacity);
};
const size_t String::s_min_capacity=15;

// 类外实现reserve（确保类名、参数、返回值和声明完全一致）
void String::reserve(size_t new_capacity) {
    // 1. 若新容量 ≤ 当前容量，无需扩容（避免无效操作）
    if (new_capacity <= capacity_) {
        return;
    }

    // 2. 分配新内存（+1是为了存储字符串结尾的'\0'，关键！）
    char* new_data = new char[new_capacity + 1];  // 容量是new_capacity，预留'\0'位置

    // 3. 拷贝旧数据到新内存（若原有数据不为空）
    if (_data != nullptr) {
        memcpy(new_data, _data, size_ + 1);  // 拷贝size_+1个字节（含原有的'\0'）
        delete[] _data;  // 释放旧内存，避免内存泄漏
    }

    // 4. 更新成员变量
    _data = new_data;       // 指向新内存
    capacity_ = new_capacity;  // 容量更新为new_capacity
}